--======================================
--  Create DELETE TRIGGER On CUSTOMER
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_CUSTOMER','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_CUSTOMER
GO

CREATE TRIGGER TRG_DEL_CUSTOMER
     ON dbo.CUSTOMER
	 FOR DELETE
AS
	DISABLE TRIGGER TRG_DEL_SAVINGBOOK ON dbo.SAVINGBOOK;
	BEGIN
		IF EXISTS(SELECT 'True' FROM Deleted d JOIN dbo.SAVINGBOOK s ON d.CustomerID = s.CustomerID)
        BEGIN
			RAISERROR('Customer has Order History. Delete failed!', 16,1)
            ROLLBACK TRAN
        END;
	END;
	ENABLE TRIGGER TRG_DEL_SAVINGBOOK ON dbo.SAVINGBOOK;
GO

--======================================
--  Create DELETE TRIGGER On SAVINGTYPE
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_SAVINGTYPE','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_SAVINGTYPE
GO

CREATE TRIGGER TRG_DEL_SAVINGTYPE
     ON dbo.SAVINGTYPE
	 FOR DELETE
AS
	DISABLE TRIGGER TRG_DEL_SAVINGBOOK ON dbo.SAVINGBOOK;
	BEGIN
		IF EXISTS(SELECT 'True' FROM Deleted d JOIN dbo.SAVINGBOOK s ON d.TypeID = s.TypeID)
        BEGIN
			RAISERROR('Saving Type has Order History. Delete failed!', 16,1)
            ROLLBACK TRAN
        END;
	END;
	ENABLE TRIGGER TRG_DEL_SAVINGBOOK ON dbo.SAVINGBOOK;
GO

--======================================
--  Create DELETE TRIGGER On SAVINGBOOK
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_SAVINGBOOK','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_SAVINGBOOK
GO

CREATE TRIGGER TRG_DEL_SAVINGBOOK
     ON dbo.SAVINGBOOK
	 FOR DELETE
AS
	BEGIN
		RAISERROR('Cannot Delete Savingbook', 16,1)
		ROLLBACK TRAN
	END
GO

--======================================
--  Create DELETE TRIGGER On DEPOSIT
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_DEPOSIT','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_DEPOSIT
GO

CREATE TRIGGER TRG_DEL_DEPOSIT
     ON dbo.DEPOSIT
	 FOR DELETE
AS
	BEGIN
		RAISERROR('Cannot Delete Deposit Order', 16,1)
		ROLLBACK TRAN
	END
GO
--======================================
--  Create DELETE TRIGGER On WITHDRAW
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_WITHDRAW','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_WITHDRAW
GO

CREATE TRIGGER TRG_DEL_WITHDRAW
     ON dbo.WITHDRAW
	 FOR DELETE
AS
	BEGIN
		RAISERROR('Cannot Delete Withdraw Order', 16,1)
		ROLLBACK TRAN
	END
GO

--======================================
--  Create DELETE TRIGGER On PARAMETER
--======================================

USE SBM
GO

IF OBJECT_ID ('TRG_DEL_PARAMETER','TR') IS NOT NULL
   DROP TRIGGER TRG_DEL_WITHDRAW
GO

CREATE TRIGGER TRG_DEL_PARAMETER
     ON dbo.PARAMETER
	 FOR DELETE
AS
	BEGIN
		RAISERROR('Cannot Delete Parameter', 16,1)
		ROLLBACK TRAN
	END
GO